package com.auto.code.generator;

import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;

public class CodeGenerator {

	 private static final Configuration configuration = new Configuration(); 
	
	public void init() {
		configuration.setClassForTemplateLoading(CodeGenerator.class, "/com/auto/code/generator/template");
	}
	
	private void render(String templateName, Map map) throws Exception {
		Template template = configuration.getTemplate(templateName);
		Writer out = new OutputStreamWriter(System.out);
		template.process(map, out);
		out.flush();
		out.close();
	}
	
	private String capital(String value) {
		String first = value.substring(0, 1).toUpperCase();
		return first + value.substring(1, value.length());
	}
	
	public void generate(String templateName, String domainName, String operationName) throws Exception {
		CodeGenerator cg = new CodeGenerator();
		cg.init();
		Map map = new HashMap();
		map.put("domainName", domainName);
		map.put("DomainName", capital(domainName));
		map.put("operationName", operationName);
		map.put("OperationName", capital(operationName));
		cg.render(templateName, map);
	}
	
	public static void main(String[] args) throws Exception {
		CodeGenerator cg = new CodeGenerator();
		cg.generate("delegate.ftl", "company", "getCompanies");	
	}
}
